package ru.fadeev.crammer.ui.window;

import java.awt.*;
import java.awt.event.*;
import java.util.Locale;

import javax.swing.*;

import ru.fadeev.crammer.Settings;
import ru.fadeev.crammer.data.WordRecord;
import ru.fadeev.crammer.ui.LimitedPlainDocument;
import ru.fadeev.crammer.ui.TransTextField;


/**
 * EditWordDialog class provides GUI for quick editing word during test. This
 * possibility is used when user during test finds errors in entered earlier data. 
 * @author Konstantin Fadeyev
 *
 */
public class EditWordDialog extends JDialog {
	// Constants
	public final static int BTN_OK = 100;
	public final static int BTN_CANCEL = 101;
	
	// UI Components
	private JPanel mainPanel;
	
	private JLabel wordLabel;
	private JTextField wordTextField;
	private JLabel transcriptionLabel;
	private JTextField transcriptionTextField;
    private JButton showTransPromptButton;

	private JLabel translationLabel;
	private JTextField translationTextField;
	private JLabel promptLabel;
	private JTextField promptTextField;
	
    protected JWindow promptWindow;
    protected Point windowLocation;
	
    private JButton okButton;
    private JButton cancelButton;
    
    // User fields
	private int pressedButton;
	private WordRecord wordRecord;
	
    
	/**
	 * Constructs EditWordDialog and initializes it with word 
	 * @param owner parent window
	 * @param word editing word
	 */
    public EditWordDialog(Dialog owner, WordRecord word) {
        super(owner,true);
        setSize(600, 175);
        Point loc = owner.getLocation();
        setLocation(loc.x + 50, loc.y + 50);
        setResizable(false);
        setTitle("Editing word");
        pressedButton = BTN_CANCEL;
        
        this.wordRecord = word;
        
        initComponents();
        initTransPrompt();
        addComponentListener(   		
        		new ComponentAdapter() {       			
        			public void componentMoved(ComponentEvent e) {
        				Point newLoc = getLocation();
       					int newPromptX = promptWindow.getX() + newLoc.x - windowLocation.x;
       					int newPromptY = promptWindow.getY() + newLoc.y - windowLocation.y;
       					promptWindow.setLocation(newPromptX, newPromptY);
        		    	windowLocation = newLoc;
        		    }
        		});
        placeComponents();
    }
    
    
    /**
     * Initializes UI Components of dialog
     */
    private void initComponents() {
        mainPanel = new JPanel();
        wordLabel = new JLabel("Word:");  
        wordTextField = new JTextField();
    	LimitedPlainDocument wordDocument = 
    		new LimitedPlainDocument(Settings.WORD_MAX_LENGTH);
    	wordTextField.setDocument(wordDocument);
    	wordTextField.setText(wordRecord.getWord());
        
        wordTextField.addFocusListener(new FocusAdapter() {
        			public void focusGained(FocusEvent e) {
        				getInputContext().selectInputMethod(Locale.US);	
        			}
        		});
        
        transcriptionLabel = new JLabel("Transcription:");  
        transcriptionTextField = new TransTextField();
    	LimitedPlainDocument transcriptionDocument = 
			new LimitedPlainDocument(Settings.TRANSCRIPTION_MAX_LENGTH);
    	transcriptionTextField.setDocument(transcriptionDocument);
        transcriptionTextField.setText(wordRecord.getTranscription());
        transcriptionTextField.addFocusListener(new FocusAdapter() {
        			public void focusGained(FocusEvent e) {
        				getInputContext().selectInputMethod(Locale.US);	
        			}
        		});
        
        translationLabel = new JLabel("Translation:");  
        translationTextField = new JTextField();
    	LimitedPlainDocument russianDocument = 
    		new LimitedPlainDocument(Settings.TRANSLATION_MAX_LENGTH);
    	translationTextField.setDocument(russianDocument);
    	translationTextField.setText(wordRecord.getTranslation());
        
        translationTextField.addFocusListener(new FocusAdapter() {
        			public void focusGained(FocusEvent e) {
        				getInputContext().selectInputMethod(new Locale("ru","RU"));	
        			}
        		});
        promptLabel = new JLabel("Prompt:");  
        promptTextField = new JTextField();
    	LimitedPlainDocument promptDocument = 
    		new LimitedPlainDocument(Settings.PROMPT_MAX_LENGTH);
    	promptTextField.setDocument(promptDocument);
    	promptTextField.setText(wordRecord.getPrompt());
        
        promptTextField.addFocusListener(new FocusAdapter() {
        			public void focusGained(FocusEvent e) {
        				getInputContext().selectInputMethod(Locale.US);	
        			}
        		});
        
		ImageIcon showTransPromptIcon = new ImageIcon("icons/16/showTransMap.png");
		showTransPromptButton = new JButton(showTransPromptIcon);
		showTransPromptButton.setToolTipText(
				"Show Transcription Prompt");     
		showTransPromptButton.addActionListener(
			    new AbstractAction() {
			        public void actionPerformed(ActionEvent ev) {
			        	promptWindow.setVisible(!promptWindow.isVisible());
			        }
			    });
		
        // Ok, Cancel buttons
        okButton = new JButton("OK");
        getRootPane().setDefaultButton(okButton);
        
		cancelButton = new JButton("Cancel");
        okButton.addActionListener(
			    new AbstractAction() {
			        public void actionPerformed(ActionEvent ev) {
			        	okButtonAction();
			        }
			    });
        
        cancelButton.addActionListener(
			    new AbstractAction() {
			        public void actionPerformed(ActionEvent ev) {
			        	cancelButtonAction();
			        }
		        });
        
        setContentPane(mainPanel);
    }
    
    
    /**
     * Places UI components using different layout managers
     */
    private void placeComponents() {
    	mainPanel.setLayout(null);
    	  	
    	wordLabel.setBounds(10, 5, 60, 20);
    	wordTextField.setBounds(80, 5, 200, 20);
    	transcriptionLabel.setBounds(10, 30, 80, 20);
    	transcriptionTextField.setBounds(80, 30, 200, 20);
    	showTransPromptButton.setBounds(280, 30, 20, 20);
    	translationLabel.setBounds(10, 55, 60, 20);
    	translationTextField.setBounds(80, 55, 200, 20);
    	promptLabel.setBounds(10, 80, 60, 20);
    	promptTextField.setBounds(80, 80, 500, 20);
    	mainPanel.add(wordLabel);
    	mainPanel.add(wordTextField);
    	mainPanel.add(transcriptionLabel);
    	mainPanel.add(transcriptionTextField);
    	mainPanel.add(showTransPromptButton);
    	mainPanel.add(translationLabel);
    	mainPanel.add(translationTextField);
    	mainPanel.add(promptLabel);
    	mainPanel.add(promptTextField);    
    	
    	okButton.setBounds(390, 115, 90, 20);
    	cancelButton.setBounds(490, 115, 90, 20);
    	mainPanel.add(okButton);
    	mainPanel.add(cancelButton);  
    }
    
    
    /**
     * Ok button action handler. Validates entered values and if all ok closes
     * dialog.
     */
    private void okButtonAction() {
    	okButton.requestFocus();
    	wordRecord.setWord(wordTextField.getText().trim());
    	wordRecord.setTranscription(transcriptionTextField.getText().trim());
    	wordRecord.setTranslation(translationTextField.getText().trim());
    	wordRecord.setPrompt(promptTextField.getText().trim());
    	
    	if(wordRecord.getWord().equals("")) {
    		JOptionPane.showMessageDialog(this, 
    				"Enter word value to continue.",
    				"Warning", JOptionPane.WARNING_MESSAGE);
    		wordTextField.requestFocus();
    		return;
    	}
    	
    	pressedButton = BTN_OK;
    	setVisible(false);
    }
    
    
    /**
     * Cancel button action handler. Closes dialog.
     */
    private void cancelButtonAction() {
        pressedButton = BTN_CANCEL;
        setVisible(false);
    }
    
    
    /**
     * After dialog is closed, call this function to find out which option
     * user picked (OK or Cancel)
     * @return pressed button (one of BTN_OK, BTN_CANCEL)
     */
    public int getPressedButton() {
    	return pressedButton;
    }
    
    
    /**
     * Overwritten, to do actions with transcription promptWindow when changing
     * visibility
     */
    public void setVisible(boolean v) {
    	if(v == false) {
	    	Settings.setBoolean("showPromptWindow", promptWindow.isVisible());
    	}
    	windowLocation = getLocation();
    	super.setVisible(v);
    }
    
    
    /**
     * Initializes TranscriptionPromptWindow (map that matches transcription 
     * symbols and keyboard keys)
     */
    public void initTransPrompt() {
    	promptWindow = new TranscriptionPromptWindow(this);
   		promptWindow.setLocation(getX() + 310, getY() - 60);
     	promptWindow.setVisible(Settings.getBoolean("showPromptWindow"));
    }
}
